package com.example.sociamedia.user.controller;

import com.example.sociamedia.user.dto.LoginRequest;
import com.example.sociamedia.user.dto.LoginResponse;
import com.example.sociamedia.user.dto.UserRequest;
import com.example.sociamedia.user.dto.UserResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    //ResponseEntity<?> 是装了网页端所需要的一些状态如404等，为前端提供接口状态
    public ResponseEntity<?> registerUser(@RequestBody UserRequest userRequest) {//Body这个注释的意思是把网页端传来的数据转化为Request形式
        try {
            UserResponse response = userService.registerUser(userRequest);// response为了可以返回给网页端注册后的值，例如让网页端知道注册成功后ID等于多少
            return new ResponseEntity<>(response, HttpStatus.CREATED);//把创建的信息和创建成功的状态码返回网页端如201 HttpStatus.CREATED
        } catch (RuntimeException e) {//把RuntimeException 这个类型实例化为 e
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);//返回e的错误信息和注册失败的状态码
        }
    }

    @GetMapping("/{userid}")
    public ResponseEntity<?> getUserProfile(@PathVariable Long userid) {
        try {
            UserResponse response = userService.getUserProfile(userid);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
        }
    }

    @PutMapping("/{userid}/profile")
    public ResponseEntity<?> updateUserProfile(@PathVariable Long userid,
                                               @RequestBody UserRequest userRequest) {
        try {
            userService.updateUserProfile(userid, userRequest);
            return new ResponseEntity<>("用户资料更新成功", HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    @PutMapping("/{userid}/password")
    public ResponseEntity<?> changePassword(@PathVariable Long userid,
                                            @RequestParam String oldPassword,
                                            @RequestParam String newPassword) {
        try {
            userService.changePassword(userid, oldPassword, newPassword);
            return new ResponseEntity<>("密码更新成功", HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping("/login")
    public ResponseEntity<?> userLogin(@RequestBody LoginRequest loginRequest) {
        try {
            LoginResponse loginresponse = userService.userLogin(loginRequest);
            return new ResponseEntity<>(loginresponse, HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
        }
    }

    @PostMapping("/logout")
    public ResponseEntity<?> logout(@RequestBody com.example.sociamedia.user.dto.LogoutRequest logoutRequest) {
        try {
            userService.logout(logoutRequest);
            return new ResponseEntity<>("登出成功", HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

}